package crypto;

import java.security.SecureRandom;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.PBEParameterSpec;

class Security {
	private SecureRandom ranGen = new SecureRandom();
	byte[] aesKey = new byte[16]; // 16 bytes = 128 bits
	//ranGen.nextBytes(aesKey);

	
	public static byte[] encrypt(byte[] data, char[] password, byte[] salt, int noIterations) {
		try {
			String method = "PBEWithMD5AndTripleDES";
			SecretKeyFactory kf = SecretKeyFactory.getInstance(method);
			PBEKeySpec keySpec = new PBEKeySpec(password);
			SecretKey key = kf.generateSecret(keySpec);
			Cipher ciph = Cipher.getInstance(method);
			PBEParameterSpec params = new PBEParameterSpec(salt, noIterations);
			return ciph.doFinal(data);
	  } catch (Exception e) {
		throw new RuntimeException("Spurious encryption error");
	  }
}
	
}